package com.muke.service;

import com.muke.common.ComConstants;
import com.muke.dao.SkillOrderRepository;
import com.muke.entity.SkillEntity;
import com.muke.entity.SkillGood;
import com.muke.entity.SkillOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.UUID;

@Service
public class SkillGoodService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkillOrderRepository skillOrderRepository;
    @Autowired
    private ProductService productService;
    @Autowired
    private MutilThreadOrder mutilThreadOrder;

    /**
     * 异步抢单
     */
    @Transactional
    public void add(Long productId, String userId) throws Exception {
        // 测试超卖
        userId= UUID.randomUUID().toString();
        // 解决重复抢单
        Long time = redisTemplate.boundHashOps(ComConstants.SKILL_GOODS_ONLY).increment(userId, 1L);
        if(time > 1){
            throw new Exception("重复抢单，不要太贪心");
        }
        // 先封装对象 并且放入redis 队列
        SkillEntity skillEntity=new SkillEntity();
        skillEntity.setProductId(productId);
        skillEntity.setUserId(userId);
        redisTemplate.boundListOps(ComConstants.SKILL_GOODS_LIST).leftPush(skillEntity);
        mutilThreadOrder.createOrder();
    }

    /**
     * 同步抢单
     */
//    @Transactional
//    public void add(Long productId, String userId) throws Exception {
//        SkillGood skillGood = productService.getGoodById(productId);
//        if (skillGood == null) {
//            throw new Exception("商品已经被抢光拉");
//        }
//        if (skillGood.getStockCount() > 0) {
//            SkillOrder skillOrder = new SkillOrder();
//            skillOrder.setMoney(skillGood.getCostPrice());
//            skillOrder.setPayTime(new Date());
//            skillOrder.setStatus("0");
//            skillOrder.setUserId(userId);
//            skillOrder.setCreateTime(new Date());
//            skillOrder.setSkillId(productId);
//            skillOrderRepository.save(skillOrder);
//            skillGood.setStockCount(skillGood.getStockCount() - 1);
//            redisTemplate.boundHashOps(SKILL_GOODS_PHONE).put(skillGood.getId(), skillGood);
//            System.out.println("成功秒杀 剩余库存："+skillGood.getStockCount());
//        }
//        if (skillGood.getStockCount() <= 0) {
//            System.out.println("库存已经是负数了:" + skillGood.getStockCount());
//            redisTemplate.boundHashOps(SKILL_GOODS_PHONE).delete(skillGood.getId());
//            productService.update(skillGood);
//        }
//    }
}
